﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TWB04
    {

        public TWB04()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBDAT();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void OnRWAUFChg(string value)
        { 
            TBMain.Clear();
            if (value.Length < 8)
            {

                return;
            }
            dwData = TBMain.NewRow();
            dwData["WBMANDANT"] = app.Mandant;
            dwData["WBRWKRS"] = value.Substring(0, 2);
            dwData["WBRWLFD"] = value.Substring(2, 6);
            SResult = ((VWBDAT)MV).QueryWBDAT(dwData, 1);
            MergeData(SResult, true);
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 12)
                return;
            dwData = TBMain.NewRow();
            dwData["WBMANDANT"] = app.Mandant;
            dwData["WBBESTKZ"] = EXAUF.Substring(0, 1);
            dwData["WBBESTKRS"] = EXAUF.Substring(1, 2);
            dwData["WBBESTLFD"] = EXAUF.Substring(3, 6);
            dwData["WBBESTPOS"] = EXAUF.Substring(9, 3);
            SResult = TMV.QWBDAT02(dwData);
            MergeData(SResult, true);
            base.OnEXAUFChg(vlaue);
        }
        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 8)
            {

                return;
            }
            dwData = TBMain.NewRow();
            dwData["WBMANDANT"] = app.Mandant;
            dwData["WBRWKRS"] = ORDERNUM.Substring(0, 2);
            dwData["WBRWLFD"] = ORDERNUM.Substring(2, 6);
            SResult = TMV.QueryWBDAT(dwData, 1);
            MergeData(SResult, true);
            base.LOADBYAUF();
        }

        public void STKNR(DataRow _dwData)
        {
            _dwData["WBBPOS"] = dwMaster["WBLESTKNR"];
            _dwData["WBBSTKNR"] = dwMaster["WBRWKRS"].ToString().Trim() + Helpers.Helper.GetSubRollNum(dwMaster["WBRWLFD"].ToString().Trim()) + "-" + Helpers.Helper.GetSubRollNum(dwMaster["WBLESTKNR"].ToString().Trim());
            dwMaster["WBLESTKNR"] = Helper.Right("0000" + (System.Convert.ToDouble(dwMaster["WBLESTKNR"].ToString()) + 1).ToString(), 4);
            _dwData["WBBURPOS"] = _dwData["WBBPOS"];
            Update();
        }
        public override void Update()
        { 
            if (dwMaster == null)
                return;
            dwMaster["WBAENDBEN"] = app.User;
            dwMaster["WBAENDDAT"] = MV.date;
            TMV.UpdateWBDAT(dwMaster, app.company, 0);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
            {
                ((VWBDAT)MV).WB02();
            }
            base.EditCommand(sender, e);
        }

    }
}
